import logging

import pytest

from resource.pom.music import Music
from utils.manager import Images


class Test:
    app = Music

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        for i in range(5):
            logging.info('步骤1.{}：进入音乐应用'.format(i))
            device.start_ability(self.app.bundle_name, self.app.ability_name)
            if i == 0:
                logging.info('预期结果1.0：有权限弹窗，切弹窗显示正常')
                win_name, _ = device.get_focus_window()
                device.assert_current_window('permissionDialog')
                device.refresh_layout()
                dialog = device.get_element_by_type('JsView')
                dialog_area = device.get_element_area(dialog)
                dialog_snapshot = device.save_snapshot_to_local('permission_dialog.jpeg')
                Images.crop_picture(dialog_snapshot, *dialog_area)
                similarity = Images.compare_image_similarity(dialog_snapshot, self.app.standard_permission_dialog)
                assert similarity > 0.6, '弹窗显示异常'
                device.click_element(device.get_element_by_text('允许'))
            logging.info('预期结果1.{}：音乐打开无卡死、黑屏、白屏等现象'.format(i))
            device.assert_current_window(self.app.window_name)
            device.refresh_layout()
            device.assert_type_exist('Slider')
            device.assert_key_exist('image1')
            device.assert_key_exist('image2')
            device.assert_key_exist('image3')
            device.assert_key_exist('image4')
            device.go_home()
            device.wait(1)
